home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE02 / TPACK / TPACK.ZIP / RESTORER.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-06-01  |  4.6 KB  |  171 lines

  1. {------------------------------------------------------------------------------}
  2. {UNREGISTERED VERSION (6/1/95) PLEASE REDISTRIBUTE IN tPACK.ZIP!
  3.  This revision does not contain everything, nor are the exciting
  4.  DataSetReporter and ExtendedMenu[Item] components included.
  5.  Use SWREG#5906 to receive these, icons and a help file for $130.
  6.  You must register when using this code in a business application!
  7.  You'll receive a license to use this code in up to 50 copies of
  8.  any app you write. In turn you will get responsive e-mail
  9.  tech support and enhancements till I run out of registrations
  10.  or suggestions. Meanwhile.. enjoy the code. Bye! I'll make more.
  11.  {(C)'1995 Michael/Ax-Systems, 71560,1754@Compuserve.com}
  12. {------------------------------------------------------------------------------}
  13.  
  14. unit Restorer;
  15.  
  16.  
  17. interface
  18.  
  19. uses Classes, Forms,
  20.   UserInfo
  21.   ,IniLink;
  22.  
  23. type
  24.  TRestorerFlag = (resLoad,resSave);
  25.  TRestorerFlags = set of TRestorerFlag;
  26.  
  27.  TGenericRestorer = class(TUserInfo)
  28.  private
  29.    fFlags: TRestorerFlags;
  30.    fFormClose: TCloseEvent;
  31.  protected
  32.    function GetActive: Boolean;
  33.    procedure SetActive(Value: Boolean); {will save when setting true in designmode}
  34.    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  35.  public
  36.    constructor Create(AOwner: TComponent); override;
  37.    destructor Destroy; override;
  38.    function UpdateOK:Boolean; Override;
  39.    procedure Save; virtual; abstract;
  40.    procedure Load; virtual; abstract;
  41.  published
  42.    property Active: Boolean read GetActive write SetActive stored false;
  43.    property Flags: TRestorerFlags read fFlags write fFlags default [resLoad,resSave];
  44.    end;
  45.  
  46.  TFormRestorer = class(TGenericRestorer)
  47.  private
  48.    fIniFileLink: TIniFileLink;
  49.  protected
  50.  public
  51.    procedure Notification(AComponent: TComponent; Operation: TOperation); Override;
  52.    procedure Save; Override;
  53.    procedure Load; Override;
  54.  published
  55.    property IniFileLink: TIniFileLink read fIniFileLink write fIniFileLink;
  56.    end;
  57.  
  58. implementation
  59.  
  60. constructor TGenericRestorer.Create(AOwner: TComponent);
  61. begin
  62.   inherited Create(AOwner);
  63.   Active:=True;
  64. end;
  65.  
  66. function TGenericRestorer.UpdateOK:Boolean;
  67. begin
  68. {  ComponentIndex:=0; }
  69.   Result:= inherited UpdateOK;
  70.   if Result and (resLoad in fFlags) then
  71.     Load;
  72.   if not cx.Designing then begin            {when initializing at runtime}
  73.     with TForm(Owner) do begin         {hook the save proc into the form's onclose handler}
  74.       fFormClose:=OnClose;
  75.       OnClose:=FormClose;
  76.       end;
  77.     end;
  78. end;
  79.  
  80. destructor TGenericRestorer.Destroy;
  81. {the action performed in here is normally not required as the form close proc has
  82. been called and we're on the way to destroying the form.. but aha! if you instantiate
  83. the component on the fly and remove it it would leave a hole in the close chain it you
  84. were not to clear the pointer here anyway. luckily the form first destroys its children,
  85. then itself, so the action here succeeds in either case.}
  86. begin
  87.   if assigned(fFormClose) then   {unhook}
  88.     with TForm(Owner) do begin
  89.       OnClose:=fFormClose;
  90.       fFormClose:=nil;
  91.       end;
  92.   inherited Destroy;
  93. end;
  94.  
  95.  
  96. function TGenericRestorer.GetActive: Boolean;
  97. begin
  98.   Result:=fFlags<>[];
  99. end;
  100.  
  101. procedure TGenericRestorer.SetActive(Value: Boolean);
  102. begin
  103.   if Value then begin
  104.     fFlags:=[resLoad,resSave];
  105.     if cx.Designing and (resSave in fFlags) then
  106.       Save;
  107.     end
  108.   else
  109.     fFlags:=[];
  110. end;
  111.  
  112. {}
  113.  
  114. procedure TGenericRestorer.FormClose(Sender: TObject;
  115.   var Action: TCloseAction);
  116. begin
  117.   if assigned(fFormClose) then
  118.     fFormClose(Sender,Action);
  119.   if (resSave in fFlags) then
  120.     Save;
  121. end;
  122.  
  123.  
  124. {}
  125.  
  126. procedure TFormRestorer.Notification(AComponent: TComponent; Operation: TOperation);
  127. begin
  128.   inherited Notification(AComponent, Operation);
  129.   if Operation = opRemove then begin
  130.     cx.NilIfSet(fIniFileLink,AComponent);
  131.     end;
  132. end;
  133.  
  134. procedure TFormRestorer.Save;
  135. var
  136.   wp: TWindowPosValue;
  137. begin
  138.   cx.MakeIfNil(fIniFileLink,TIniFileLink);
  139.   with tForm(Owner) do begin
  140.     wp.Top:=Top;
  141.     wp.Left:=Left;
  142.     wp.Height:=Height;
  143.     wp.Width:=Width;
  144.     end;
  145.   with fIniFileLink do begin
  146.     Section:='WindowPos';
  147.     WindowPosEntry[owner.classname]:=wp;
  148.     end;
  149. end;
  150.  
  151. procedure TFormRestorer.Load;
  152. var
  153.   wp: TWindowPosValue;
  154. begin
  155.   cx.MakeIfNil(fIniFileLink,TIniFileLink);
  156.   with fIniFileLink do begin
  157.     Section:='WindowPos';
  158.     wp:=WindowPosEntry[owner.classname];
  159.     end;
  160.   if wp.Width>0 then
  161.     with tForm(Owner) do begin
  162.       Top:=wp.Top;
  163.       Left:=wp.Left;
  164.       Height:=wp.Height;
  165.       Width:=wp.Width;
  166.       end;
  167. end;
  168.  
  169.  
  170. end.
  171.